import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export interface NotificationItem {
    id: string
    title: string
    content: string
    time: string
    type: 'info' | 'warning' | 'success' | 'error'
    isRead: boolean
    details?: string[]
    affectedFunctions?: string[]
    remark?: string
}

export const useNotificationStore = defineStore('notification', () => {
    // 通知数据
    const announcementList = ref<NotificationItem[]>([
        {
            id: '1',
            title: '系统维护通知',
            content: '系统将于今晚22:00-24:00进行维护升级，期间可能影响部分功能使用。',
            time: '2024-06-15 10:30',
            type: 'warning',
            isRead: false,
            details: [
                '数据库优化',
                '系统性能提升',
                '安全补丁更新'
            ],
            affectedFunctions: [
                '用户登录',
                '订单处理',
                '支付功能'
            ],
            remark: '请提前做好相关工作安排，感谢您的理解与支持。'
        },
        {
            id: '2',
            title: '新功能上线',
            content: '推荐算法优化功能已上线，支持更精准的商品推荐。',
            time: '2024-06-14 16:20',
            type: 'success',
            isRead: false,
            details: [
                '智能推荐算法',
                '个性化标签功能',
                '用户行为分析'
            ],
            remark: '新功能将为您提供更好的用户体验。'
        },
        {
            id: '3',
            title: '数据备份提醒',
            content: '请及时备份重要数据，系统将于本周日进行数据清理。',
            time: '2024-06-13 09:15',
            type: 'warning',
            isRead: true,
            details: [
                '历史订单数据',
                '用户行为数据',
                '系统日志数据'
            ],
            remark: '建议在清理前导出重要数据。'
        },
        {
            id: '4',
            title: '安全更新通知',
            content: '系统安全补丁已更新，修复了多个安全漏洞。',
            time: '2024-06-12 14:30',
            type: 'info',
            isRead: true,
            details: [
                'SQL注入防护',
                'XSS攻击防护',
                'CSRF攻击防护'
            ],
            remark: '建议所有用户及时更新。'
        }
    ])

    const systemErrorList = ref<NotificationItem[]>([
        {
            id: '5',
            title: '数据库连接异常',
            content: '检测到数据库连接不稳定，已自动切换到备用服务器。技术人员正在处理中。',
            time: '2024-06-15 08:45',
            type: 'error',
            isRead: false
        },
        {
            id: '6',
            title: '缓存服务异常',
            content: 'Redis缓存服务出现异常，系统性能可能受到影响。已启动自动恢复机制。',
            time: '2024-06-14 14:30',
            type: 'error',
            isRead: false
        },
        {
            id: '7',
            title: 'API接口超时',
            content: '第三方支付接口响应超时，已启用备用支付通道。支付功能暂时受限。',
            time: '2024-06-13 11:20',
            type: 'error',
            isRead: true
        },
        {
            id: '8',
            title: '文件上传失败',
            content: '文件上传服务出现异常，部分文件上传功能暂时不可用。正在紧急修复中。',
            time: '2024-06-12 16:45',
            type: 'error',
            isRead: true
        }
    ])

    // 计算未读通知数量
    const totalUnreadCount = computed(() => {
        const announcementUnread = announcementList.value.filter(item => !item.isRead).length
        const systemUnread = systemErrorList.value.filter(item => !item.isRead).length
        return announcementUnread + systemUnread
    })

    // 标记单个通知为已读
    function markAsRead(id: string, type: 'announcement' | 'system') {
        const list = type === 'announcement' ? announcementList.value : systemErrorList.value
        const item = list.find(item => item.id === id)
        if (item) {
            item.isRead = true
        }
    }

    // 标记全部为已读
    function markAllAsRead() {
        announcementList.value.forEach(item => item.isRead = true)
        systemErrorList.value.forEach(item => item.isRead = true)
    }

    return {
        announcementList,
        systemErrorList,
        totalUnreadCount,
        markAsRead,
        markAllAsRead
    }
}) 